<?php
namespace app\app\controller\v1;
use app\app\Appbase;
use app\admin\model\SystemParamValue;
use base\date\Dates;
//
//预约
//
class UserYy extends Appbase 
{
	
	/**
	* 获取员工预约时间
	* /app/v1/user_yy/getYyTime
	* 需要登录 
	* @is_need_login true
	*/
	public function getYyTime($staff_id){
		$shop_id = request()->shopId;
		try {
			
			$day = model('shop_scheduling')
				->where('shop_id',$shop_id)
				->where('year',date("Y"))
				->where('month',date("m"))
				->where('staff_id',$staff_id)
				->value('days');
			$days = explode(',',$day);	
			
			$dates =  Dates::getWeeksDays();
			foreach($dates as $key => $val ){
				$dates[$key]['is_work'] = !$day ? true : in_array($val['day'],$days);
			}
			
			$data = [
				"dates"=>$dates,
				"staff"=> model('ShopStaff')
					->where("id",$staff_id)
					->withAvg(['evaluate'=>function($query){
					    $query->where('is_del',0)->field('staff_id,service_score');
					    return 'star';
					}],'service_score')
					->field('id,gh,name,img,phone,work_type,sex,time')
					->find()
			];
			
			$data['staff']['time'] = $data['staff']['time'] ?: model('Shop')
				->where("id",$shop_id)
				->value('time') ?: '';
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取员工信息
	* /app/v1/user_yy/getShopInfo
	* 需要登录 
	* @is_need_login true
	*/
	public function getStaffInfo($staff_id){
		try {
			$result = model('ShopStaff')->get($staff_id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 我的预约
	* /app/v1/user_yy/getList
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function getList($status='all'){
		try {
			$res = model("ShopYyOrder")->getUserYy($status);
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取预约单明细
	* /app/v1/user_yy/details
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function details($id){
		if(!$id) return $this->ReError('参数错误！');
		try {
			$res = model("ShopYyOrder")->getModel()->get($id);
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*取消预约
	* /app/v1/user_yy/cancel
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function cancel($id){
		try {
			$res = model("ShopYyOrder")->cancel($id);
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
		
	/**
	*评价
	* /app/v1/user_yy/evaluate
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function evaluate($order_id,$service_score,$comment)
    {
    	if(!$order_id || !$service_score || !$comment) return $this->ReError('参数错误！');
		try {
			if(!$order = model("ShopYyOrder")->get($order_id)){
				return $this->ReError('订单不存在！');	
			}
			if( $order->evaluate()->find() ){
				return $this->ReError('该订单已评价！');	
			}
			$order->star = $service_score;
			$order->save();
			$order->evaluate()->save([
				'order_id'=>$order_id,
				'service_score' =>$service_score,
				'comment' =>$comment
			]);
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
	
	
	/**
	*已购项目
	* /app/v1/user_yy/ygProj
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function ygProj($staff_id='')
    {
		try {
			$shopId = -1;
			if($staff_id){
				$shopId =  model('ShopStaff')->where('id',$staff_id)->value('shop_id');
			}
			$proj = model("UserYgProj")->getYgProj(request()->userId,$shopId);
			$service = model("shop_staff_service")->where('staff_id',$staff_id)->column('specs_id');
			foreach($proj as $key=>$val){
				$val['is_service'] = in_array($val['specs_id'],$service);
			}
			return $this->ReSucess($proj);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
    
    
    /**
	*获取-购买新项目信息
	* /app/v1/user_yy/gmProjInfo
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function gmProjInfo()
    {
    	$shop_id = request()->shopId;
		try {
			//TODO 项目banner图
	        $banner = SystemParamValue::getSlides('buess_index_banner','shop_id_'.$shop_id);
	        if( !$banner[0] ){
	        	$banner = SystemParamValue::getSlides('buess_index_banner','shop_id_0');
	        }
			//TODO 分类列表
	        $cate_ids =  model("goods")->getModel()->where('a.type',2)->column('a.cate_id');
	        $cates =  model("goodsCate")->where('id','in',$cate_ids)->field('id,name')->select();
	        //返回
	        return $this->ReSucess(compact('banner','cates') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
	
	
	/**
	*获取-购买新项目信息-列表
	* /app/v1/user_yy/gmProjList
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function gmProjList($cate_id)
    {
		try {
	        $model =  model("goods")->getModel();
	        $model =  $model ->where('a.type',2);
	        if($cate_id) $model =  $model ->where('a.cate_id',$cate_id);
	        $data =  $model ->all();
	        //返回
	        return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
    
    /**
	*提交预约单
	* /app/v1/user_yy/submit
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function submit()
    {
		try {
			$param = request()->post();
			$param['uid'] = request()->userInfo['id'];
			
			$param['shop_id'] = request()->userInfo['shop_id'];
			if($param['proj_id']){
				$param['shop_id'] = model('user_yg_proj')->where('id',$param['proj_id'])->value('shop_id');
			}
			
			$proj = model('UserYgProj')->alias('a')
				->leftJoin('goods_specs b','a.specs_id = b.id')
				->where('a.id',$param['proj_id'])
				->field('a.*,(a.num - a.use_num) as surplus,b.service_score')
				->find();
				
			if( !$proj ){
				return $this->ReError('已购项目不存在！');	
			}
			
			if( $proj['surplus'] < 1 ){
				return $this->ReError('当前项目可预约次数已用完！');	
			}
			
			$service = model("shop_staff_service")->where('staff_id',$param['staff_id'])->column('goods_id');
			if( !in_array($proj['goods_id'],$service) ){
				return $this->ReError('该员工不服务此项目！');	
			}

			$expireTime = strtotime("+$proj[effective_time] months",strtotime($proj['add_time']) );
			if( $proj->meal_id > 0 && time() >$expireTime ){
				return $this->ReError('已购项目已过期！');	
			}
			
			$order =  model('ShopYyOrder')
				->where('uid',$param['uid'])
				->where('status','in',"0,3")
				->where('proj_id',$param['proj_id'])
				->order('id','desc')
				->find();
			if( $order->status == 0){
				return $this->ReError('您存在预约订单未完成，请取消或完成后再预约！',-2);	
			}
			
			$param['service_score'] = $proj->service_score ?: 0;
			if( !model("ShopYyOrder")->allowField(true)->save($param) ){
				return $this->ReError('异常错误！');	
			}
			
	        //返回
	        return $this->ReSucess(model("ShopYyOrder")->id,'预约成功！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
	
	
	/**
	*过期自动完成
	*/  
    public function autoComplete()
    {
        $result = model('common/ShopYyOrder')->autoComplete();
		if(!$result){
		  	return false;
		}else{
			return true;	
		}
    }
    
}



